import sql from './'
(async function() {
  const id = 666
  let { data: [userData] } = await sql.selectSql(`
    select "username" from "userList"
    WHERE "_id" = "${id}"
  `)

  let aa = await sql.executeSql([
    "create table if not exists msg('toId' CHAR(32),'fromId' CHAR(32),'text' VARCHAR(255),'time' DATETIME)",
    "insert into msg values('" + data.toId + "','" + data.fromId + "','" + data.text + "','" + data.time + "')"
  ])

  await executeSql([
    "create table if not exists msg('fromIdR(32),'senderId' CHAR(32),'text' VARCHAR(255),'time' DATETIME)",
    "insert into msg values('" + data.fromId + "','" + data.senderId + "','" + data.text + "','" + data.time + "')"
  ])

  let aa = await sql.executeSql([
    "create table if not exists msg('isRead' INTEGER,'fromId' CHAR(32),'toId' CHAR(32),'text' VARCHAR(255),'img' VARCHAR(255),'voice' VARCHAR(255),'video' VARCHAR(255),'timestamp' INTEGER)",
    'insert into msg values(' + isRead + ",'" + payload.fromId + "','" + payload.toId + "','" + payload.text + "','" + payload.img + "','" + payload.voice + "','" + payload.video + "','" + payload.timestamp + "')"
  ])

  let { data } = await sql.selectSql(`
    select timestamp,text,fromId,count(fromId) as num
    from msg
    WHERE isRead = 0
    GROUP BY fromId
  `)

  // 先全部归零
  await sql.executeSql('UPDATE userList SET "num" = 0')
  // 将对应的打上 数字 内容 时间
  data.forEach(async ({ num, fromId, text, timestamp }) => {
    console.log(text, num)
    let res = await sql.executeSql(`
      UPDATE
        "userList"
      SET
        "num" = ${num},
        "text" = "${text}",
        "timestamp" = ${timestamp}
      WHERE
        "_id" = "${fromId}"
    `)
  })

  let { data: userList } = await sql.selectSql('select * from "userList" ORDER BY timestamp DESC')

  // 数据库增加该记录
  await sql.executeSql([					// 编号，手机号/用户名，最后一次消息内容，最后一次会话时间，未读数量
    "create table if not exists userList('_id' CHAR(32),'username' VARCHAR(255),'text' VARCHAR(255),'timestamp' INTEGER,'num' INTEGER)",
    "insert into userList values('" + user._id + "','" + user.username + "','" + user.text + "','" + user.timestamp + "','" + user.num + "')"
  ])

  let res = await sql.executeSql(`
    UPDATE
      "userList"
    SET
      "num" = 0,
      "text" = "${payload.text}",
      "timestamp" = ${payload.timestamp}
    WHERE
      "_id" = "${payload.fromId}"
  `)

  let { data } = await sql.selectSql(`select * from msg WHERE
    ("fromId" = "${this.currentUserId}" AND "toId" = "${this.mineId}")
    OR
    ("toId" = "${this.currentUserId}" AND "fromId" = "${this.mineId}")
    ORDER BY "timestamp" DESC
  `)

  let res = await sql.executeSql(`
    UPDATE
      "userList"
    SET
      "num" = 0,
      "text" = "${text}",
      "timestamp" = ${Date.now()}
    WHERE
      "_id" = "${this.currentUserId}"
  `)

  let a = await sql.executeSql([
    "create table if not exists msg('isRead' INTEGER,'fromId' CHAR(32),'toId' CHAR(32),'text' VARCHAR(255),'img' VARCHAR(255),'voice' VARCHAR(255),'video' VARCHAR(255),'timestamp' INTEGER)",
    "insert into msg values(1,'" + this.mineId + "','" + this.currentUserId + "','" + text + "','" + img + "','" + voice + "','" + video + "','" + Date.now() + "')"
  ])

  let { data: [data] } = await sql.selectSql(`
    select  count(*) as num
    from msg
    WHERE isRead = 0
  `)
})()

